#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int n = nums.size(), len = -1;
        int sum = 0;

        for (int e : nums)
            sum += e;//2
        int target = sum - x, temp = 0; // -1
        if (target < 0) return -1;
        int left = 0, right = 0;
        while (right < n)
        {
            temp += nums[right];
            while (temp > target)// 1 > target == -1
            {
                temp -= nums[left];
                left++;
            }
            if (temp == target)
            {
                len = max(len, right - left + 1);
            }
            right++;
        }
        if (len == -1) return -1;
        else return n - len;
    }
};